﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using EnglishRegister.Business;
using EnglishRegister.Utilities;
namespace EnglishRegister.Forms
{

    public partial class frmCourseDetail : Form
    {
        private Guid CourseID;

        private EnglishRegister.Entities.CourseModel course;

        private Form _parentForm;

        public Form ParentForm
        {
            set
            {
                if (value is Form)
                {
                    this._parentForm = value;
                }
            }
        }
        public frmCourseDetail()
        {
            InitializeComponent();
            this.CourseID = Guid.Empty;
            this.cmbLevel.Items.Clear();
            this.cmbLevel.DataSource = Enum.GetNames(typeof(CourseLevel));

            this.cmbTimeLearning.Items.Clear();
            this.cmbTimeLearning.DataSource = Enum.GetNames(typeof(CourseLearnTime));
        }

        public frmCourseDetail(Guid courseID)
            : this()
        {
            this.CourseID = courseID;


        }

        private void LoadDetail()
        {
            this.course = CourseBL.GetByPrimaryKey(this.CourseID);
            this.tbxCourseName.Text = course.Name;
            this.dtpEnd.Value = course.EndTime.Value;
            this.tbxFee.Text = course.Tuition.Value.ToString();
            this.tbxHour.Text = course.TotalHours.Value.ToString();
            this.cmbLevel.SelectedIndex = course.Level == null ? 0 : course.Level.Value;
            this.cmbTimeLearning.SelectedIndex = course.TimeStudy == null ? 0 : course.TimeStudy.Value;
            this.tbxLimitPeople.Text = course.LimitPeople.Value.ToString();
            this.dtpStart.Value = course.StartTime.Value;
            this.tbxDescription.Text = course.Description;

        }



        private bool CheckValid()
        {
            string strName = this.tbxCourseName.Text;
            if (string.IsNullOrEmpty(strName))
            {
                Utilities.UtilitiesTools.SetErrorFor(this.tbxCourseName, this.eprCourse, "Cannot be null or Empty");
                return false;
            }
            else if (CourseBL.GetByName(strName) != null)
            {
                Utilities.UtilitiesTools.SetErrorFor(this.tbxCourseName, this.eprCourse, "This name Existed.");
            }
            else if (true)
            {

            }

            return true;
        }

        private void GetAllCourseInfo()
        {
            this.course.Name = this.tbxCourseName.Text;
            this.course.Level = (short)this.cmbLevel.SelectedIndex;
            this.course.LimitPeople = Convert.ToInt32(this.tbxLimitPeople.Text);
            this.course.StartTime = this.dtpStart.Value;
            this.course.EndTime = this.dtpEnd.Value;
            this.course.TotalHours = short.Parse(this.tbxHour.Text);
            this.course.Tuition = int.Parse(this.tbxFee.Text);
            this.course.TimeStudy = (short)this.cmbTimeLearning.SelectedIndex;
            this.course.Description = this.tbxDescription.Text;
        }

        private void GoBack()
        {
            this._parentForm.Activate();
            this._parentForm.Show();
            this.Close();
        }
        #region **Event**
        private void frmCourseDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_parentForm != null)
            {
                this._parentForm.Activate();
                this._parentForm.Show();
            }
        }

        private void frmCourseDetail_Load(object sender, EventArgs e)
        {
            if (this.CourseID != Guid.Empty)
            {
                this.LoadDetail();
            }
        }

        private bool CheckNameExisted(string strCourseName)
        {

            if (CourseBL.GetByName(strCourseName) != null)
            {
                UtilitiesTools.ShowErrorMessage("Course with name existed");
                return false;
            }
            return true;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string txtCourseName = this.tbxCourseName.Text;
            try
            {
                if (this.eprCourse.DataSource != null) return;

                if (this.CourseID != Guid.Empty)
                {
                    this.GetAllCourseInfo();
                    if (txtCourseName != this.course.Name && CheckNameExisted(this.course.Name)) return;
                    if (StudentBL.GetAllStudentByCourse(CourseID).Count > this.course.LimitPeople)
                    {
                        UtilitiesTools.SetErrorFor(this.tbxLimitPeople, this.eprCourse, "Student has registered. This number cannot be less than that number");
                        return;
                    }
                    CourseBL.UpdateCourse(this.course);
                    this.course = null;
                }
                else
                {
                    this.course = new Entities.CourseModel();
                    this.GetAllCourseInfo();
                    if (!CheckNameExisted(tbxCourseName.Text)) return;
                    CourseBL.InsertCourse(this.course);
                }
                this.GoBack();
            }
            catch (Exception ex)
            {
                UtilitiesTools.ShowErrorMessage(ex.Message);
            }

        }

        private void TextBox_Validating(object sender, CancelEventArgs e)
        {
            TextBox txtText = sender as TextBox;
            Regex regex = new Regex(@"^\d+$");
            if (!regex.IsMatch(txtText.Text))
            {
                UtilitiesTools.SetErrorFor(txtText, this.eprCourse, "Number only!");
            }
            else
                this.eprCourse.Clear();
        }

        private void btnRegist_Click(object sender, EventArgs e)
        {

        }

        private void DateTime_Validating(object sender, CancelEventArgs e)
        {
            DateTimePicker control = sender as DateTimePicker;
            if (dtpEnd.Value < dtpStart.Value)
            {
                UtilitiesTools.SetErrorFor(control, this.eprCourse, "Wrong Start and End time");
            }
        }

    }
        #endregion
}
